package com.example.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ChenXiaoBin
 * on 2019/6/21
 */

@RestController
@RequestMapping("/V1.0/random/time")
public class RandomTimeController {
    public static void main(String[] args) {
//        for (int i=0;i<30;i++){
//            Date date = randomDate("2019-01-01","2019-01-31");
//            System.out.println(new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date));
//        }
        Date date = randomDate("2019-01-01 08-00-00","2019-01-01 09-00-00");
        System.out.println(new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date));
    }

    private static Date randomDate(String beginDate,String endDate){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
            Date start = format.parse(beginDate);
            Date end = format.parse(endDate);
            if(start.getTime() >= end.getTime()){
                return null;
            }
            long date = random(start.getTime(),end.getTime());
            return new Date(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static long random(long begin,long end){
        long rtn = begin + (long)(Math.random() * (end - begin));
        if(rtn == begin || rtn == end){
            return random(begin,end);
        }
        return rtn;
    }

}
